package com.zl.controller;

import com.zl.service.PrivilegeService;
import com.zl.service.Role_PrivilegeService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PrivilegeController {
    @Autowired
    private PrivilegeService privilegeService;
    @Autowired
    private Role_PrivilegeService role_privilegeService;

    @RequestMapping(value = "/privilege/privileges", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject privileges() {
        return new JSONObject().accumulate("privileges", privilegeService.findAll());
    }

    @RequestMapping(value = "/privilege/privilege", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject privilege(int rid) {
        return new JSONObject().accumulate("privilege", role_privilegeService.findByRId(rid));
    }


    @RequestMapping(value = "/privilege/setPrivilege", method = RequestMethod.POST)
    public
    @ResponseBody
    JSONObject setPrivilege(String privileges, int rid) {
        return new JSONObject().accumulate("message", (role_privilegeService.setPrivileges(privileges.split("\\,"), rid) == 0) ? "设置失败！！" : "设置成功！");
    }

}
